package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class Practice10HistogramView extends View {

    private int width;
    private int height;
    private Paint paintTitle;
    private String title = "直方图";
    private Rect rectTitle;
    private Paint paintLine;
    private Paint paintText;
    private Rect[] rectText = new Rect[7];
    private String[] text = new String[]{
            "兰尼斯特",
            "坦格利安",
            "史塔克",
            "拜拉席恩",
            "雪诺",
            "夜王",
            "龙妈",
    };
    private float[] gaodu = new float[]{
            0.05f,
            0.1f,
            0.1f,
            0.4f,
            0.6f,
            0.8f,
            0.3f
    };
    private int zWidth;
    private int jiange;
    private Paint paintZhiFang;

    public Practice10HistogramView(Context context) {
        super(context);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        zWidth = dp_px(30);
        jiange = dp_px(8);
        //初始化标题画笔
        paintTitle = new Paint();
        paintTitle.setColor(Color.WHITE);
        paintTitle.setTextSize(dp_px(15));
        rectTitle = new Rect();
        paintTitle.getTextBounds(title, 0, title.length(), rectTitle);
        //初始化横竖坐标线画笔
        paintLine = new Paint();
        paintLine.setColor(Color.WHITE);
        paintLine.setStyle(Paint.Style.STROKE);
        paintLine.setStrokeWidth(dp_px(1));
        //初始化底部文字画笔
        paintText = new Paint();
        paintText.setTextSize(dp_px(8));
        paintText.setColor(Color.WHITE);
        for (int i = 0; i < text.length; i++) {
            rectText[i] = new Rect();
            paintText.getTextBounds(text[i], 0, text[i].length(), rectText[i]);
        }
        //初始化直方画笔
        paintZhiFang = new Paint();
        paintZhiFang.setColor(Color.GREEN);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        综合练习
//        练习内容：使用各种 Canvas.drawXXX() 方法画直方图

        //画标题
        canvas.drawText(title, width / 2 - rectTitle.width() / 2, height - rectTitle.height(), paintTitle);
        //画坐标系
        Path path = new Path();
        path.moveTo(width / 10, height / 10);
        path.lineTo(width / 10, height / 10 * 8);
        path.rLineTo(width / 10 * 8, 0);
        canvas.drawPath(path, paintLine);
        //画底部文字
        for (int i = 0; i < text.length; i++) {
            canvas.drawText(text[i], width / 10 + jiange * (i+1) + zWidth / 2 * (2 * (i+1) - 1) - rectText[i].width() / 2, height / 10 * 8 + rectText[i].height() * 2, paintText);
        }
//        canvas.drawText(text[0], width / 10 + jiange * 1 + zWidth / 2 * (2 * 1 - 1) - rectText[0].width() / 2, height / 10 * 8 + rectText[0].height() * 2, paintText);
//        canvas.drawText(text[1], width / 10 + jiange * 2 + zWidth / 2 * (2 * 2 - 1) - rectText[1].width() / 2, height / 10 * 8 + rectText[1].height() * 2, paintText);
//        canvas.drawText(text[2], width / 10 + jiange * 3 + zWidth / 2 * (2 * 3 - 1) - rectText[2].width() / 2, height / 10 * 8 + rectText[2].height() * 2, paintText);
        //画直方
        for (int i = 0; i < rectText.length; i++) {
            canvas.drawRect(width / 10 + jiange * (i + 1) + zWidth * i, height / 10 * 8 - height / 10 * 7 * gaodu[i], width / 10 + jiange * (i + 1) + zWidth * (i + 1), height / 10 * 8, paintZhiFang);
        }
//        canvas.drawRect(width/10+jiange*2+zWidth,height/10*8-height/10*7*gaodu[1],width/10+jiange*2+zWidth*2,height/10*8,paintZhiFang);
    }

    /**
     * dp转px
     *
     * @param values
     * @return
     */
    public int dp_px(int values) {

        float density = getResources().getDisplayMetrics().density;
        return (int) (values * density + 0.5f);
    }

}
